package com.company.section1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        RealSubject r = new RealSubject("lalala");
        InvocationHandler handler = ProxyTest.test1(r);
        Object o = Proxy.newProxyInstance(r.getClass().getClassLoader(), r.getClass().getInterfaces(), handler);
        Method method=o.getClass().getMethod("doSomething",String.class);
        //执行该方法
        Object result = method.invoke(o, "wawawaw");
        System.out.println(result);

    }
    public static InvocationHandler test1(Object object){
        class MyInvocationHandlertest implements InvocationHandler{
            Object obj = null;
            public MyInvocationHandlertest(Object object){
                this.obj = object;
            }
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("前置");
                method.invoke(obj,args);
                return null;
            }
        }
        return new MyInvocationHandlertest(object);
    }
}
